package com.google.ma.rssreader;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class AddRssActivity extends Activity {

    private EditText titleEditText;

    private EditText urlEditText;

    private Button comfirmBtn;
    
    private Button cancelBtn;

    private RssDAO rssDAO;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_rss);
        titleEditText = (EditText) findViewById(R.id.AddRssTitleEditText);
        titleEditText.setText("");
        urlEditText = (EditText) findViewById(R.id.AddRssUrlEditText);
        urlEditText.setText("");
        comfirmBtn = (Button) findViewById(R.id.AddRssOKButton);

        comfirmBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String title = titleEditText.getText().toString();
                String url = urlEditText.getText().toString();
                if (Utils.isNotEmpty(title) && Utils.isNotEmpty(url)) {
                    rssDAO = new RssDAO(AddRssActivity.this);
                    if (rssDAO.insert(title, url)) {
                        new AlertDialog.Builder(AddRssActivity.this).setIcon(R.drawable.icon).setTitle(R.string.add_rss_success).setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton) {
                                Intent intent = new Intent();
                                intent.setClass(AddRssActivity.this, NewsSelectActivity.class);
                                finish();
                            }
                        }).create().show();
                    }
                } else {
                    if (Utils.isEmpty(title)) {
                        new AlertDialog.Builder(AddRssActivity.this).setIcon(R.drawable.icon).setTitle(R.string.add_rss_title_no_empty).create().show();
                    } else {
                        new AlertDialog.Builder(AddRssActivity.this).setIcon(R.drawable.icon).setTitle(R.string.add_rss_url_no_empty).create().show();
                    }
                }
            }
        });

        cancelBtn = (Button) findViewById(R.id.AddRssCancelButton);

        cancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}
